package com.sailor.excel.controller;

import com.sailor.excel.constant.FileTypeEnum;
import com.sailor.excel.easyExcel.EasyExcelHelp;
import com.sailor.excel.entity.Question;
import com.sailor.excel.excelTemplate.EasyExcelQuestionTemplate;
import com.sailor.excel.excelTemplate.QuestionExcelTemplate;
import com.sailor.excel.service.QuestionService;


import com.sailor.excel.util.MyBeanUtil;
import com.sailor.excel.vo.QuestionVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Controller
public class ExcelController {

    @Resource
    private QuestionService questionService;

    @GetMapping("/getExcel")
    public void getExcel(HttpServletResponse response) throws Exception {
        // 打印sql  关键字字段处理
        List<Question> questionList = questionService.list(null);
        List<QuestionVO> questionVOList = MyBeanUtil.convertList2List(questionList, QuestionVO.class);

        response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        String fileName = URLEncoder.encode("导出测试" + FileTypeEnum.EXCEL.getMessage(), StandardCharsets.UTF_8.toString());
        response.setHeader("content-disposition", "attachment;filename=" + fileName);
        QuestionExcelTemplate.getReportExcel(response.getOutputStream(), questionVOList);
    }

    @GetMapping("/getEasyExcel")
    public void getEasyExcel(HttpServletResponse response) throws Exception {
        List<Question> questionList = questionService.list(null);
        List<EasyExcelQuestionTemplate> easyExcelQuestionTemplateList = MyBeanUtil.convertList2List(questionList, EasyExcelQuestionTemplate.class);
        EasyExcelHelp.commonExport(response, "导出测试", easyExcelQuestionTemplateList, EasyExcelQuestionTemplate.class);

    }

}
